import {
	Request,
	SUCCESS_CODE
} from '@/utils/request.js';
import {
	Plat,
	APPID,
	SECRET
} from '@/utils/config.js';
import {
	Toast
} from '@/utils/common.js';


var WXBizDataCrypt = require('@/utils/WXBizDataCrypt.js');

export const User = {
	clear() {
		//清空本地所有缓存
		uni.clearStorageSync();
	},
	/**
	 * @param {Object} val (包含:nickName,token,avatar)
	 */
	setInfo(val) {
		uni.setStorageSync("user", val);
	},
	/**
	 * 设置位置信息
	 * @param {Object} val
	 */
	setLocation(val) {
		uni.setStorageSync("location", val);
	},
	getLocation() {
		let value = uni.getStorageSync("location");
		if (value != null) {
			return value;
		}
		return null;
	},
	getToken() {
		let value = uni.getStorageSync("user");
		if (value != null) {
			return value.token;
		}
		return null;
	},
	getNickName() {
		let value = uni.getStorageSync("user");
		if (value != null) {
			return value.nickName;
		}
		return null;
	},
	getId() {
		let value = uni.getStorageSync("user");
		if (value != null) {
			return value.id;
		}
		return null;
	},
	//是否已经登录
	isLogin() {
		let user = uni.getStorageSync("user");
		if (user && user.token) {
			return true;
		}
		return false;
	},
	/**
	 * 获取登录的code
	 */
	getCode() {
		return new Promise((resolve, reject) => {
			wx.login({
				success(res) {
					return resolve(res.code);
				},
				fail(res) {
					return reject(res);
				},
			});
		});
	},
	//通过获取用户code登录
	login() {
		return new Promise((resolve, reject) => {
			wx.login({
				success(res) {
					//console.log("微信登录结果：", res)
					if (res.errMsg == "login:ok") {
						//console.log("成功获取临时code,临时code为：", res.code);
						//调用后端登录接口
						Request.post('/api/member/login', {
							code: res.code,
							type: 'code', //登录方式
							plat: Plat,
						}).then((res) => {
							console.log("返回结果=====>", res);
							User.setInfo(res);
							resolve(res);
						});
					} else {
						uni.showToast({
							title: "微信code获取失败",
						})
					}
				}
			})
		});
	},
	//获取用户资料(读取服务器信息)
	getProfile() {
		return new Promise((resolve, reject) => {
			Request.get("/api/member/profile", {}).then((res) => {
				resolve(res);
			});
		});
	},
	//更新服务器端会员位置信息
	updateLocation(postData) {
		return new Promise((resolve, reject) => {
			Request.post("/api/member/location/update", postData).then((res) => {
				resolve(res);
			});
		});
	},
	//获取用户当前位置信息
	location() {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'wgs84',
				geocode: true, //设置该参数为true可直接获取经纬度及城市信息
				success(res) {
					console.log("当前用户位置信息======>", res);
					/* User.setLocation({
						latitude:res.latitude,//纬度
						longitude:res.longitude,//经度
					}); */
					//resolve(res);
					let lat = res.latitude;
					let lng = res.longitude;
					Request.get("/api/common/location", {
						lat: lat,
						lng: lng
					}).then((res) => {
						console.log("获取到的百度地理位置数据========>", res);
						if (res && res.city) {
							User.setLocation(res);
							User.updateLocation(res);
							resolve(res);
						} else {
							reject("fail");
						}
					});
				},
				fail(res) {
					Toast.error("位置获取失败");
					reject("fail");
				}
			})
		});
	},
	//退出系统
	exitSystem() {
		User.clear();
		uni.navigateTo({
			url: '/pages/login/index'
		});
	},
	/**
	 * 绑定手机号码
	 * @param {Object} phone
	 * @param {Object} captcha
	 */
	bindPhone(phone, captcha) {
		return new Promise((resolve, reject) => {
			Request.post("/api/member/phone/bind", {
					phone: phone,
					captcha: captcha
				}).then((res) => resolve(res))
				.catch((res) => reject(res));
		});
	},

 /**
	* 是否开店
	*/
 hasOpenShop(){
	 return new Promise((resolve,reject)=>{
		  Request.get("/api/member/hasOpenShop",{}).then((res)=>{
				resolve(res);
			})
	 });
 },

}
